package mtn.bowen.yan.server.utils;

/**
 * @author <a href=mailto:yanbowen90@foxmail.com> bowen.yan </a>
 * @version 1.0
 * @description: To internationalization
 * @date 2021-10-22 9:47 AM
 */

import mtn.bowen.yan.server.common.Constant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

public final class ResourceBundleUtil {

    private final static Logger LOGGER = LoggerFactory.getLogger(ResourceBundleUtil.class);

    /**
     * language 资源
     */
    private static final ResourceBundle resource;

    static {

//        resource = ResourceBundle.getBundle("messages", Locale.ENGLISH);
        resource = ResourceBundle.getBundle("messages", Locale.CHINA);
    }

    /**
     * 私有构造函数
     */
    private ResourceBundleUtil() {

    }

    /**
     * Gets a string from the ResourceBundles.
     * <br> Convenience method to save casting.
     *
     * @param key the key of the properties.
     * @return the value of the property. Return the key if the value is not found.
     */
    public static String getStringValue(String key) {
        try {
            return resource.getString(key);
        } catch (MissingResourceException e) {
            return Constant.NO_RESOURCE_DEFINE;
        }
    }

    /**
     * Gets the integer from the properties.
     *
     * @param key the key of the property.
     * @return the value of the key. return -1 if the value is not found.
     */
    public static Integer getIntegerValue(String key) {
        try {
            return Integer.valueOf(resource.getString(key));
        } catch (MissingResourceException e) {
            return new Integer(-1);
        }
    }

    public static ResourceBundle getResource() {
        return resource;
    }
}